#include "stdafx.h"
#include "wf_drawarc.h"



WF_DrawArc::WF_DrawArc(PF_Container& container,
                                   PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw arcs",
                           container, graphicView) {

    reset();
}



WF_DrawArc::~WF_DrawArc() {}

QAction* WF_DrawArc::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// (tr("Arc: Center, Point, Angles")
    QAction* action = new QAction(tr("&Center, Point, Angles"),NULL);
	action->setIcon(QIcon(":/extui/arcscraa.png"));
    //action->zetStatusTip(tr("Draw arcs"));
    return action;
}


void WF_DrawArc::reset() {
    //bool rev = data.reversed;

    if (data.reversed) {
        data = PF_ArcAttribute(PF_Vector(false),
                          0.0,
                          2*M_PI, 0.0,
                          true);
    } else {
        data = PF_ArcAttribute(PF_Vector(false),
                          0.0,
                          0.0, 2*M_PI,
                          false);
    }
}



void WF_DrawArc::init(int status) {
    PF_PreviewActionInterface::init(status);

    reset();
}



void WF_DrawArc::trigger() {
    PF_PreviewActionInterface::trigger();

    PF_Arc* arc = new PF_Arc(container,
                             data);
    arc->setLayerToActive();
    arc->setPenToActive();
    container->addEntity(arc);

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        document->addUndoable(arc);
        document->endUndoCycle();
    }

	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(arc->getCenter());

    setStatus(SetCenter);
    reset();

    PF_DEBUG->print("WF_DrawArc::trigger(): arc added: %d",
                    arc->getId());
}



void WF_DrawArc::mouseMoveEvent(QMouseEvent* e) {
    PF_DEBUG->print("WF_DrawArc::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);
    switch (getStatus()) {
    case SetCenter:
        data.center = mouse;
        break;

    case SetRadius:
        if (data.center.valid) {
            data.radius = data.center.distanceTo(mouse);
            deletePreview();
            preview->addEntity(new PF_Circle(preview,
                                             PF_CircleAttribute(data.center,
                                                           data.radius)));
            drawPreview();
        }
        break;

    case SetAngle1:
        data.angle1 = data.center.angleTo(mouse);
        if (data.reversed) {
            data.angle2 = PF_Math::correctAngle(data.angle1-M_PI/3);
        } else {
            data.angle2 = PF_Math::correctAngle(data.angle1+M_PI/3);
        }
        deletePreview();
        preview->addEntity(new PF_Arc(preview,
                                      data));
        drawPreview();
        break;

    case SetAngle2:
        data.angle2 = data.center.angleTo(mouse);
        deletePreview();
        preview->addEntity(new PF_Arc(preview,
                                      data));
        drawPreview();
        break;

    case SetIncAngle:
        data.angle2 = data.angle1 + data.center.angleTo(mouse);
        deletePreview();
        preview->addEntity(new PF_Arc(preview,
                                      data));
        drawPreview();
        break;

    case SetChordLength: {
            double x = data.center.distanceTo(mouse);
            if (fabs(x/(2*data.radius))<=1.0) {
                data.angle2 = data.angle1 + asin(x/(2*data.radius)) * 2;
                deletePreview();
                preview->addEntity(new PF_Arc(preview,
                                              data));
                drawPreview();
            } 
        }
        break;

    default:
        break;

    }

    PF_DEBUG->print("WF_DrawArc::mouseMoveEvent end");
}



void WF_DrawArc::mouseReleaseEvent(QMouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawArc::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }
    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetCenter:
        data.center = mouse;
        graphicView->moveRelativeZero(mouse);
        setStatus(SetRadius);
        break;

    case SetRadius:
        if (data.center.valid) {
            data.radius = data.center.distanceTo(mouse);
        }
        setStatus(SetAngle1);
        break;

    case SetAngle1:
        data.angle1 = data.center.angleTo(mouse);
        setStatus(SetAngle2);
        break;

    case SetAngle2:
        data.angle2 = data.center.angleTo(mouse);
        trigger();
        break;

    case SetIncAngle:
        data.angle2 = data.angle1 + data.center.angleTo(mouse);
        trigger();
        break;

    case SetChordLength: {
            double x = data.center.distanceTo(mouse);
            if (fabs(x/(2*data.radius))<=1.0) {
                data.angle2 = data.angle1 + asin(x/(2*data.radius)) * 2;
                trigger();
            }
        }
        break;

    default:
        break;
    }
}



void WF_DrawArc::commandEvent(PF_CommandEvent* e) {
    QString c = e->getCommand().toLower();

    if (PF_COMMANDS->checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    if (PF_COMMANDS->checkCommand("reversed", c)) {
        e->accept();
        setReversed(!isReversed());

        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->requestOptions(this, true, true);
        }
        return;
    }

    switch (getStatus()) {

    case SetRadius: {
            bool ok;
            double r = PF_Math::eval(c, &ok);
            if (ok==true) {
                data.radius = r;
                setStatus(SetAngle1);
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
        }
        break;

    case SetAngle1: {
            bool ok;
            double a = PF_Math::eval(c, &ok);
            if (ok==true) {
                data.angle1 = PF_Math::deg2rad(a);
                setStatus(SetAngle2);
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
        }
        break;

    case SetAngle2: {
            if (PF_COMMANDS->checkCommand("angle", c)) {
                setStatus(SetIncAngle);
            } else if (PF_COMMANDS->checkCommand("chord length", c)) {
                setStatus(SetChordLength);
            } else {
                bool ok;
                double a = PF_Math::eval(c, &ok);
                if (ok==true) {
                    data.angle2 = PF_Math::deg2rad(a);
                    trigger();
                } else {
                    if (PF_DIALOGFACTORY!=NULL) {
                        PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                    }
                }
            }
        }
        break;

    case SetIncAngle: {
            bool ok;
            double a = PF_Math::eval(c, &ok);
            if (ok==true) {
                data.angle2 = data.angle1 + PF_Math::deg2rad(a);
                trigger();
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
        }
        break;

    case SetChordLength: {
            bool ok;
            double l = PF_Math::eval(c, &ok);
            if (ok==true) {
                if (fabs(l/(2*data.radius))<=1.0) {
                    data.angle2 = data.angle1 + asin(l/(2*data.radius)) * 2;
                    trigger();
                } else {
                    if (PF_DIALOGFACTORY!=NULL) {
                        PF_DIALOGFACTORY->commandMessage(
                            tr("Not a valid chord length"));
                    }
                }
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
        }
        break;

    default:
        break;
    }
}



QStringList WF_DrawArc::getAvailableCommands() {
    QStringList cmd;
    cmd += PF_COMMANDS->command("reversed");
    return cmd;
}


void WF_DrawArc::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetCenter:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify center"), tr("Cancel"));
            break;
        case SetRadius:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify radius"), tr("Back"));
            break;
        case SetAngle1:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify start angle:"), tr("Back"));
            break;
        case SetAngle2:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify end angle or [Angle/chord Length]"),
                tr("Back"));
            break;
        case SetIncAngle:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify included angle:"),
                                                tr("Back"));
            break;
        case SetChordLength:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify chord length:"),
                                                tr("Back"));
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DrawArc::showOptions() {
    PF_ActionInterface::showOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true);
    }
}



void WF_DrawArc::hideOptions() {
    PF_ActionInterface::hideOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, false);
    }
}



void WF_DrawArc::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawArc::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        if (!isFinished()) {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        } else {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarArcs);
        }
    }
}


// EOF

